package org.senseframework.support.example.mybatis.service;


import org.senseframework.support.core.dto.PageData;
import org.senseframework.support.core.spring.SpringApplicationContext;
import org.senseframework.support.orm.mybatis.mysql.AbstractMySqlDao;
import org.senseframework.support.orm.mybatis.query.builder.DBBuilder;

import java.util.List;

/**
 * minzhou2 on 2016/10/24.
 */
public abstract class AbstractService {

    public <DAO extends AbstractMySqlDao> DAO dao() {
        return SpringApplicationContext.getBean(daoName());
    }

    public abstract String daoName();

    public int update(DBBuilder updateBuilder) {
        return dao().update(updateBuilder);
    }

    public int insert(DBBuilder insertBuilder) {
        return dao().insert(insertBuilder);
    }

    public int count(DBBuilder queryBuilder) {
        return dao().count(queryBuilder);
    }

    public <T> List<T> list(DBBuilder queryBuilder) {
        return dao().list(queryBuilder);
    }

    public <T> T one(DBBuilder queryBuilder) {
        return (T) dao().one(queryBuilder);
    }

    public <K> K sum(DBBuilder queryBuilder) {
        return (K) dao().sum(queryBuilder);
    }

    public PageData page(DBBuilder queryBuilder) {
        PageData pageData = new PageData();
        pageData.setCount(count(queryBuilder));
        pageData.setList(list(queryBuilder));
        pageData.setPageNumber(queryBuilder.getPage());
        pageData.setPageSize(queryBuilder.getSize());
        return pageData;
    }

}
